package com.jsptags.navigation.pager;

import com.jsptags.navigation.pager.parser.IndexTagExport;
import com.jsptags.navigation.pager.parser.ParseException;
import com.jsptags.navigation.pager.parser.TagExportParser;
import javax.servlet.jsp.*;

@SuppressWarnings("serial")
public final class IndexTag extends PagerTagSupport {

	private String export;
	private IndexTagExport indexTagExport;
	private Object oldItemCount;
	private Object oldPageCount;

	public final void setExport(String s) throws JspException {
		if (export != s)
			try {
				indexTagExport = TagExportParser.parseIndexTagExport(s);
			} catch (ParseException parseexception) {
				throw new JspTagException(parseexception.getMessage());
			}
		export = s;
	}

	public final String getExport() {
		return export;
	}

	public final int doStartTag() throws JspException {
		super.doStartTag();
		if (indexTagExport != null) {
			String s;
			if ((s = indexTagExport.getItemCount()) != null) {
				oldItemCount = pageContext.getAttribute(s);
				pageContext.setAttribute(s,
						new Integer(pagerTag.getItemCount()));
			}
			if ((s = indexTagExport.getPageCount()) != null) {
				oldPageCount = pageContext.getAttribute(s);
				pageContext.setAttribute(s,
						new Integer(pagerTag.getPageCount()));
			}
		}
//		pagerTag.isIndexNeeded();
		return 1;
	}

	public final int doEndTag() throws JspException {
		if (indexTagExport != null) {
			String s;
			if ((s = indexTagExport.getItemCount()) != null) {
				restoreAttribute(s, oldItemCount);
				oldItemCount = null;
			}
			if ((s = indexTagExport.getPageCount()) != null) {
				restoreAttribute(s, oldPageCount);
				oldPageCount = null;
			}
		}
		super.doEndTag();
		return 6;
	}

	public final void release() {
		export = null;
		indexTagExport = null;
		oldItemCount = null;
		oldPageCount = null;
		super.release();
	}

	public IndexTag() {
		export = null;
		indexTagExport = null;
		oldItemCount = null;
		oldPageCount = null;
	}
}
